/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.cxhd.lis.util.DateUtil;
import com.cxhd.lis.util.ToolDateTime;
import com.jfinal.core.Controller;
import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisUser extends Model<LisUser> {

    public static final LisUser lisUserdao = new LisUser();

    public void deleteUser(int pk) {
        System.out.println("删除" + pk);
        Db.use("mysql").update("delete from lis_user where pk=" + pk);

    }

    public void updateUser(Controller con) {
//        Db.use("mysql").update("update", paras)

        findById(con.getPara("pk"))
                .set("name", con.getPara("name"))
                .set("birth", con.getPara("birth"))
                .set("address", con.getPara("adress"))
                .set("phone", con.getPara("phone"))
                .set("sex", con.getPara("sex"))
                .set("department", con.getPara("department"))
                .set("position", con.getPara("position"))
                 .set("sign", con.getPara("sign"))
                .update();
    }
//    user_id name birth address phone sex department position

    public void addUser(String user_id,
            String name, String birth, String address, String phone,
            String sex, String department, String position, String data1, String data2, String data3, String password) {
        // System.out.println(Integer.parseInt(getPk())+"//"+user_id+"//"+name+"//"+birth+"//"+address+"//"+phone+"//"+sex+"//"
        //  +department+"//"+position+"//"+data1+"//"+data2+"//"+data3);
//        ToolDateTime da=new ToolDateTime();
//         Date time=DateUtil.dateFormat(da.);   
        java.util.Date date = new java.util.Date();
        String time = DateUtil.dateFormat(date);

        // set("pk",Integer.parseInt(getPk()))
        set("user_id", user_id)
                .set("name", name)
                .set("birth", birth)
                .set("address", address)
                .set("phone", phone)
                .set("sex", sex)
                .set("department", department)
                .set("position", position)
                //                创建时间
                .set("createtime", time)
                .set("data1", data1)
                .set("data2", data2)
                .set("data3", data3)
                .set("password", HashKit.md5(user_id + password))
                .save();

    }

    public String getPk() {
        String value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(pk) as id from lis_user ").get("id") + "") + 1) + "";
        return value;
    }

    public boolean checkId(String userId) {
        // System.out.println(findFirst("select * from lis_user where user_id=? ",userId)+"----------------------");
        return findFirst("select * from lis_user where user_id=? ", userId) != null;

    }

    public LisUser checkLogin(String userId, String password) {

        // System.out.println("select * from lis_user where user_id='"+userId+"' and password='"+HashKit.md5(userId+password)+"' ");
        return findFirst("select a.*,b.pk as role_id from lis_user a left join lis_system_role b on a.position=b.name  where user_id=? and password=? ", userId, HashKit.md5(userId + password));

    }

    public void editPW(String user_id, String pw, String pk) {
        System.out.println(user_id+" == "+pw+"==="+pk);

        findById(pk)
                .set("password", HashKit.md5(user_id + pw))
                .update();

    }

}
